package com.hn.Util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 两个时间差计算
 */
public class DateUtil {



    /**
     * 两个时间之间相差距离多少天
     * @param // 时间参数 1：
     * @param // 时间参数 2：
     * @return 相差天数
     */
    public static long getDistanceDays(String starttime, String endtime,String typename ) throws Exception{
        DateFormat df = new SimpleDateFormat("yyyy:MM:dd");
        Date one;
        Date two;
        long days=0;
        if (typename.equals(".*天.*")){

            try {
                one = df.parse(starttime);
                two = df.parse(endtime);
                long time1 = one.getTime();//当前
                long time2 = two.getTime();//过去
                long diff ;
                if(time1<time2) {
                    diff = time2 - time1;
                } else {
                    diff = time1 - time2;
                }
                days = diff / (1000 * 60 * 60 * 24);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        System.err.println("时间差"+days);
        return days;//返回相差多少天
    }


    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     * @param starttime 时间参数1 格式：1990-01-01 12:00:00
     * @param endtime 时间参数 2 格式：2009-01-01 12:00:00
     * @typename 查询类型
     * @return long 返回值为：{天, 时, 分, 秒}
     */
    public static long getJetlag(String starttime, String endtime,String typename) {
        long day = 0;//天
        long hour = 0;//小时
        long min = 0;//分
        long sec = 0;//秒
        long date = 0;
        long time1 = Long.parseLong(starttime);//当前Long.parseLong("String")返回long基本数据类型
        long time2 =  Long.parseLong(endtime);//当前
        long diff ;
            diff = time1 - time2;
        if (typename.equals("天")) {
            day = diff / (24 * 60 * 60 * 1000);
           date = day;
        }
        if (typename.equals("时")) {
            hour = (diff / (60 * 60 * 1000) - day * 24);
            date = hour;
        }
        if (typename.equals("分")) {
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            date = min;
        }
        if (typename.equals("秒")) {
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            date = sec;
        }
        return date;
    }



    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     * @param //str1 时间参数 1 格式：1990-01-01 12:00:00
     * @param //str2 时间参数 2 格式：2009-01-01 12:00:00
     * @return String 返回值为：xx天xx小时xx分xx秒
     */
    public static String getDistanceTime(String starttime, String endtime) {
        DateFormat df = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;

        try {
            one = df.parse(starttime);
            two = df.parse(endtime);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff ;
            if(time1<time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day + "天" + hour + "小时" + min + "分" + sec + "秒";
    }
}